#ifndef MQTT_PUSH_CONSOLE_H
#define MQTT_PUSH_CONSOLE_H

#include "mqtt_push.h"

class Mqtt_push_console
{
public:
    Mqtt_push_console() {};
    ~Mqtt_push_console()
    {
        std::lock_guard<std::mutex> lock(mqtt_push_mtx_);
        for (auto &it : mqtt_push_map_)
        {
            it.second->stop();
        }
    };

    int get_channel_num();
    void add_channel(std::string url, std::string topic);
    void push_img(int channel_idx, std::shared_ptr<FrameInfoDetectYolov8Seg> img_ptr);

private:
    std::unordered_map<int, std::shared_ptr<MqttPush>> mqtt_push_map_;
    std::mutex mqtt_push_mtx_;
    int que_size();
};

int Mqtt_push_console::get_channel_num()
{
    std::lock_guard<std::mutex> lock(mqtt_push_mtx_);
    return mqtt_push_map_.size();
}

void Mqtt_push_console::add_channel(std::string url, std::string topic)
{

    int channel_idx = get_channel_num();
    mqtt_push_map_.insert(std::make_pair(channel_idx, std::make_shared<MqttPush>(url, topic, std::string("mqtt_push_console_" + std::to_string(channel_idx)))));
    mqtt_push_map_[channel_idx]->start();
    std::cout << "mqtt channel add success, url: " << url << ", topic: " << topic << std::endl;
}

void Mqtt_push_console::push_img(int channel_idx, std::shared_ptr<FrameInfoDetectYolov8Seg> img_ptr)
{
    if(channel_idx < 0 || channel_idx >= get_channel_num())
    {
        std::cout << "mqtt channel idx error, channel_idx: " << channel_idx << std::endl;
        return;
    }
    mqtt_push_map_[channel_idx]->push_img(channel_idx, img_ptr);
}

#endif // MQTT_PUSH_CONSOLE_H